

var express =require("express");
var router = express.Router();
var {Mvs,Prouser,Xiangmu,Xiaoqu,Xueke,Banji,Xuekexiangmu} = require("../utils/model")
var {createToken, checkToken} = require("../utils/token")
router.get("/test",(req,res)=>{
    res.json({
        code:200,
        msg:"测试接口 成功 successful "
    })
})  

router.get("/movie",(req,res)=>{
    var limit  = req.query.limit *1 || 0;
    Mvs.find({},{_id:0})
    .limit(limit)
    .sort({_id:-1})
    .then(result=>{
        res.json({
            code:200,
            msg:"获取数据成功",
            result
        })
    })
})

router.post("/login",(req,res)=>{
    var body = req.body;
    console.log(body);
    res.json({
        code:200,
        msg:"登录成功...",
        body
    })
})


// 获取 crsftoken 
router.get("/gettoken",(req,res)=>{
    // var token = createToken('wuhan');
    res.json({
        token:"token",
        msg:"获取token成功",
        type:1,
        code:200
    })
})

// 登录 
router.post("/signin",(req,res)=>{
    var body = req.body
    console.log(body);
    Prouser.findOne({
        mobile:body.mobile,
    }).then(result=>{
        if(result){
            if(result.password == body.password){
                var token  = createToken({
                    mobile:body.mobile,
                    password:body.password,
                    roleid:result.roleid
                })
                res.json({
                    code:200,
                    type:1,
                    msg:"登录成功",
                    token
                })
            }else{
                res.json({
                    code:200,
                    type:0,
                    msg:"登录失败,用户名和密码不匹配"
                })
            }
        }else{
            res.json({
                code:200,
                type:0,
                msg:"登录失败,用户名和密码不匹配"
            })
        }
    })
})

//注册

router.post("/addyonghu",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        var body = req.body;
        Prouser.findOne({
                mobile:body.mobile
            }).then(data=>{
                if(data){
                    res.json({
                        code:402,
                        type:0,
                        msg:"手机号码已经存在,请确认数据"
                    })
                }else{
                    body.time = new Date();
                    Prouser.insertMany(body)
                    .then(result=>{
                        res.json({
                            code:200,
                            type:1,
                            result,
                            msg:"用户添加成功"
                        })
                    })
                }
            })
        
    })
})




//获取个人信息  token
router.post("/getuserinfo",(req,res)=>{
    checkToken(req,res,({mobile})=>{
        Prouser.findOne({
            mobile:mobile
        }).then(result=>{
            res.json({
                code:200,
                msg:'获取信息成功',
                result:result,
                type:1
            })
        })



       
    })
})



//获取首页banner
router.post("/gethomebanner",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        res.json({
            msg:"获取数据成功",
            code:200,
            type:1,
            result:[
                {
                    imgUrl:"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2049256090,1152074654&fm=26&gp=0.jpg",
                    text:"努力拼搏"
                },
                {
                    imgUrl:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599797778451&di=989a2bdef3b66e83c542bbe17621d838&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F2018-01-26%2F5a6a9f36a705b.jpg",
                    text:"顽强奋发"
                },
                {
                    imgUrl:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599797833182&di=c407d20159f86a60c4cad1a4acf8384c&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F2017-12-11%2F5a2e47f0a6c22.jpg",
                    text:"积极进取"
                },
                {
                    imgUrl:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599797745827&di=5d586bb81a754b7e0346a924c33547ab&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2Ff%2F57c39d755f767.jpg",
                    text:"敢为人先"
                }
            ]
        })
    })
})




// 提交项目
router.post("/addxiangmu",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        console.log(req.body);
        Prouser.findOne({
            mobile,
        }).then(userinfo=>{
            Xiangmu.insertMany({
                time:new Date(),
                mobile,
                username:userinfo.username,
                reply:'',
                ...req.body
            }).then(result=>{
                res.json({
                    code:200,
                    msg:"添加成功",
                    result,
                    type:1
                })    
            }).catch(err=>{
                res.json({
                    code:500,
                    msg:"服务器异常",
                    err,
                    type:0
                })
            })
        })
    })
})


// 获取项目列表  

router.post("/getxiangmulist",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        Xiangmu.find({},{})
        .sort({_id:-1})
        .then(result=>{
            res.json({
                code:200,
                msg:"获取项目列表成功",
                result,
                type:1
            })
        })
        .catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
                result,
                err,
                type:0
            })
        })
    })
})

// 根据_id 删除项目 
router.post("/delxiangmu",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        Xiangmu.remove({
            _id:req.body._id
        })
        .then(result=>{
            res.json({
                code:200,
                msg:"删除项目成功",
                result,
                type:1
            })
        })
        .catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
                result,
                err,
                type:0
            })
        })
    })
})


//项目打分

router.post("/setxmscore",(req,res)=>{
    var body = req.body;
    Xiangmu.updateMany({
        _id:body._id
    },{
        $set:{
            score:body.score
        }
    }).then(result=>{
        res.json({
            code:200,
            msg:"项目打分成功",
            type:1,
            result
        })
    })
    .catch(err=>{
        res.json({
            msg:"服务器异常",
            code:500,
            type:0,
        })
        throw err;
    })
})



// 根据_id 修改项目 
router.post("/updatexiangmu",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        var body = {...req.body}
        // delete body[_id];
        Xiangmu.updateMany({
            _id:req.body._id
        },{
            $set:body
        })
        .then(result=>{
            res.json({
                code:200,
                msg:"修改成功",
                result,
                type:1
            })
        })
        .catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
                result,
                err,
                type:0
            })
        })
    })
})


// 根据mobile修改个人信息 
router.post("/updategeren",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        var body = {...req.body}
        // delete body[_id];
        Prouser.insertMany({
            mobile:req.body.mobile
        },{
            $set:body
        })
        .then(result=>{
            res.json({
                code:200,
                msg:"修改成功",
                result,
                type:1
            })
        })
        .catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
                result,
                err,
                type:0
            })
        })
    })
})



// 新增校区
router.post("/addxiaoqu",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        // console.log(req.body);
        var body = req.body;
        console.log(body)
        Prouser.findOne({
            mobile,
        }).then(result=>{
            Xiaoqu.insertMany(body)
            .then(result=>{
                res.json({
                    code:200,
                    msg:"添加成功",
                    result,
                    type:1
                })    
            }).catch(err=>{
                res.json({
                    code:500,
                    msg:"服务器异常",
                    err,
                    type:0
                })
            })
        })
    })
})


// 获取校区
router.post("/getxiaoqulist",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        Xiaoqu.find({},{})
        .sort({_id:-1})
        .then(result=>{
            res.json({
                code:200,
                msg:"获取项目列表成功",
                result,
                type:1
            })
        })
        .catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
              
                err,
                type:0
            })
        })
    })
})


// 删除校区
router.post("/delxiaoqu",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        Xiaoqu.remove({
            _id:req.body._id
        })
        .then(result=>{
            res.json({
                code:200,
                msg:"删除项目成功",
                result,
                type:1
            })
        })
        .catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
                result,
                err,
                type:0
            })
        })
    })
})



// 新增学科
router.post("/addxueke",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        // console.log(req.body);
        var body = req.body;
        console.log(body)
        Prouser.findOne({
            mobile,
        }).then(result=>{
            Xueke.insertMany(body)
            .then(result=>{
                res.json({
                    code:200,
                    msg:"添加学科成功",
                    result,
                    type:1
                })    
            }).catch(err=>{
                res.json({
                    code:500,
                    msg:"服务器异常",
                    err,
                    type:0
                })
            })
        })
    })
})



// 获取学科
router.post("/getxuekelist",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        Xueke.find({},{})
        .sort({_id:-1})
        .then(result=>{
            res.json({
                code:200,
                msg:"获取学科列表成功",
                result,
                type:1
            })
        })
        .catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
              
                err,
                type:0
            })
        })
    })
})


// 删除学科
router.post("/delxueke",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        Xueke.remove({
            _id:req.body._id
        })
        .then(result=>{
            res.json({
                code:200,
                msg:"删除学科成功",
                result,
                type:1
            })
        })
        .catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
                result,
                err,
                type:0
            })
        })
    })
})



// 新增班级
router.post("/addbanji",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        // console.log(req.body);
        var body = req.body;
        console.log(body)
        Prouser.findOne({
            mobile,
        }).then(result=>{
            Banji.insertMany(body)
            .then(result=>{
                res.json({
                    code:200,
                    msg:"新增成功",
                    result,
                    type:1
                })    
            }).catch(err=>{
                res.json({
                    code:500,
                    msg:"服务器异常",
                    err,
                    type:0
                })
            })
        })
    })
})




// 获取班级
router.post("/getbanjilist",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        Banji.find({},{})
        .sort({_id:-1})
        .then(result=>{
            res.json({
                code:200,
                msg:"获取班级列表成功",
                result,
                type:1
            })
        })
        .catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
              
                err,
                type:0
            })
        })
    })
})


// 删除班级
router.post("/delbanji",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        Banji.remove({
            _id:req.body._id
        })
        .then(result=>{
            res.json({
                code:200,
                msg:"删除班级成功",
                result,
                type:1
            })
        })
        .catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
                result,
                err,
                type:0
            })
        })
    })
})



// 新增学科项目
router.post("/addxkxiangmu",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        // console.log(req.body);
        var body = req.body;
        console.log(body)
        Prouser.findOne({
            mobile,
        }).then(result=>{
            Xuekexiangmu.insertMany(body)
            .then(result=>{
                res.json({
                    code:200,
                    msg:"新增学科项目成功",
                    result,
                    type:1
                })    
            }).catch(err=>{
                res.json({
                    code:500,
                    msg:"服务器异常",
                    err,
                    type:0
                })
            })
        })
    })
})


// 获取学科项目列表
router.post("/getxkxiangmulist",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        Xuekexiangmu.find({},{})
        .sort({_id:-1})
        .then(result=>{
            res.json({
                code:200,
                msg:"获取班级项目列表成功",
                result,
                type:1
            })
        })
        .catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
              
                err,
                type:0
            })
        })
    })
})



// 删除学科项目
router.post("/delxuekexiangmu",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        Xuekexiangmu.remove({
            _id:req.body._id
        })
        .then(result=>{
            res.json({
                code:200,
                msg:"删除学科项目成功",
                result,
                type:1
            })
        })
        .catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
                result,
                err,
                type:0
            })
        })
    })
})


//添加用户

router.post("/adduser",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        var body = req.body;
        var addroleid = body.roleid;
        if(addroleid>roleid){
            res.json({
                code:403,
                type:0,
                msg:"你权限不够，不能添加超级管理员"
            })
        }else{

            //添加用户
            Prouser.findOne({
                mobile:body.mobile
            }).then(data=>{
              if(data){
                res.json({
                    code:402,
                    type:0,
                    msg:"手机号码已经存在，请确认数据"
                })
              }else{
                  body.time=new Date();
                    Prouser.insertMany(body)
                    .then(result=>{
                        res.json({
                            code:200,
                            type:1,
                            result,
                            msg:"用户添加成功"
                        
                        })
                    })
              }
            })
        }
    })
})



//查询用户
router.post("/getuserlist",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        Prouser.find({},{})
        .sort({_id:-1})
        .then(result=>{
            res.json({
                code:200,
                msg:"获取用户列表成功",
                result,
                type:1
            })
        })
        .catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
                result,
                err,
                type:0
            })
        })
    })
})


//根据_id删除用户
router.post("/deluser",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
      Prouser.remove({
          _id:req.body._id
      })
      .then(result=>{
        res.json({
            code:200,
            msg:"用户删除成功",
            result,
            type:1
        })
      }).catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
                err,
                type:0
            })
        })
    })
})



//根据_id修改用户
router.post("/updateuserone",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        var body = {...req.body,time:new Date()}
        // delete body[_id];
      Prouser.updateMany({
          _id:req.body._id
      },{
          $set:body
      })
      .then(result=>{
        res.json({
            code:200,
            msg:"用户修改成功",
            result,
            type:1
        })
      }).catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
                err,
                type:0
            })
        })
    })
})




//查询项目
router.post("/getXMlist",(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        var body = req.body;
    console.log(body);
        Xiangmu.find({
            // category:body
        })
        .sort({_id:-1})
        .then(result=>{
            res.json({
                code:200,
                msg:"查询成功",
                result,
                type:1
            })
        })
        .catch(err=>{
            res.json({
                code:500,
                msg:"服务器异常",
                result,
                err,
                type:0
            })
        })
    })
})


module.exports = router;